// 2874. 有序三元组中的最大值 II
// AC: https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii/submissions/618565344/

class Solution {
public:
    long long maximumTripletValue(vector<int>& nums) {
        long long ans=0;
        int n=nums.size();
        vector<int> res(n,0);
        vector<int> pre(n,0);
        res[n-1]=nums[n-1];
        pre[0]=nums[0];
        for(int i=n-2;i>=0;--i){
             res[i]=max(res[i+1],nums[i]);
        }
    for(int i=1;i<n;++i)
        pre[i]=max(pre[i-1],nums[i]);
        for(int i=1;i<n-1;++i){
            ans=max(ans,(long long)(pre[i-1]-nums[i])*res[i+1]);
        }
        return ans;
    }
};